package com.cloudsigns.factorymanager2.produce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.produce.adapter.ProduceDispatchOrderAdapter;
import com.cloudsigns.factorymanager2.produce.bean.Produce_ProductBean;
import com.cloudsigns.factorymanager2.produce.iview.IGetProductOrderAssignListView;
import com.cloudsigns.factorymanager2.produce.presenter.GetProductOrderAssignListPresenter;
import com.cloudsigns.factorymanager2.project.customview.DropDownPoupWindow;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 分派工单
 */
public class ProduceDispatchOrderListActivity extends BaseActivity implements IGetProductOrderAssignListView {
    private RelativeLayout rl_back, rl_head;
    private TextView tv_title;
    private View ll_op_one, ll_op_two, ll_op_three, ll_op_four;
    private ImageView img_op_one, img_op_two, img_op_three, img_op_four;
    private ImageView img_tri_one, img_tri_two, img_tri_three, img_tri_four;
    private View rl_progressbar;
    private ListView lv;

    /*项目选择*/
    public static final int BTN_SELECT = 0;
    private DropDownPoupWindow popupWindow;

    private ProduceDispatchOrderAdapter adapter;
    private List<Produce_ProductBean> productAssignList = new ArrayList<>();

    private GetProductOrderAssignListPresenter getProductOrderAssignListPresenter;


    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_dispatch_order_list);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        rl_head = findViewById(R.id.rl_head);
        ll_op_one = findViewById(R.id.ll_op_one);
        ll_op_two = findViewById(R.id.ll_op_two);
        ll_op_two = findViewById(R.id.ll_op_two);
        ll_op_three = findViewById(R.id.ll_op_three);
        ll_op_four = findViewById(R.id.ll_op_four);
        img_op_one = findViewById(R.id.img_op_one);
        img_op_two = findViewById(R.id.img_op_two);
        img_op_three = findViewById(R.id.img_op_three);
        img_op_four = findViewById(R.id.img_op_four);
        img_tri_one = findViewById(R.id.img_tri_one);
        img_tri_two = findViewById(R.id.img_tri_two);
        img_tri_three = findViewById(R.id.img_tri_three);
        img_tri_four = findViewById(R.id.img_tri_four);
        rl_progressbar = findViewById(R.id.rl_progressbar);
        lv = findViewById(R.id.lv);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.dispatch_list));

        img_op_one.setBackgroundResource(R.mipmap.title_icon_menu);
        img_op_two.setBackgroundResource(R.mipmap.icon_hand);
        img_op_three.setBackgroundResource(R.mipmap.icon_produced);
        img_op_four.setBackgroundResource(R.mipmap.icon_patrol);

        ll_op_one.setVisibility(View.GONE);
        ll_op_two.setVisibility(View.GONE);
        ll_op_three.setVisibility(View.GONE);
        ll_op_four.setVisibility(View.VISIBLE);
        rl_back.setVisibility(View.VISIBLE);
        rl_head.setVisibility(View.GONE);

        adapter = new ProduceDispatchOrderAdapter(this, TAG);
        adapter.setData(productAssignList);
        lv.setAdapter(adapter);

        getProductOrderAssignListPresenter = new GetProductOrderAssignListPresenter(this);
        getProductOrderAssignListPresenter.getProductOrderAssignList();

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Produce_ProductBean produce_productBean = productAssignList.get(position);
                Intent mIntent = new Intent(ProduceDispatchOrderListActivity.this, ProduceDispatchOrderDetailActivity.class);
//                Intent mIntent = new Intent(ProduceDispatchOrderListActivity.this, ProduceDispatchOrderDetailActivity1.class);
                mIntent.putExtra(ConstantUtil.KEY_1, produce_productBean);
                startActivity(mIntent);
            }
        });
    }

    @Override
    public void setListener() {
        ll_op_one.setOnClickListener(this);
        ll_op_two.setOnClickListener(this);
        ll_op_three.setOnClickListener(this);
        ll_op_four.setOnClickListener(this);
        rl_back.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        Intent mIntent;
        switch (v.getId()) {
            case R.id.rl_back:
                finish();
                break;
        }
    }

    @Override
    public void getProductOrderAssignListSuccessed(List<Produce_ProductBean> productList) {
        if (null != productList && productList.size() > 0) {
            productAssignList.clear();
            productAssignList.addAll(productList);
            adapter.setData(productAssignList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getProductOrderAssignListFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {
        rl_progressbar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        rl_progressbar.setVisibility(View.GONE);
    }
}
